package com.example.jh352160.surfingtv.mypage;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.mypage.childPage.About_us_page;
import com.example.jh352160.surfingtv.mypage.childPage.AdvicePage;
import com.example.jh352160.surfingtv.mypage.childPage.EditPage;
import com.example.jh352160.surfingtv.mypage.childPage.Favorites;
import com.example.jh352160.surfingtv.mypage.childPage.MypageSetting;
import com.example.jh352160.surfingtv.mypage.customView.MypageItem;
import com.example.jh352160.surfingtv.mypage.customView.ProfileImageView;
import com.example.jh352160.surfingtv.network.item.LoginItem;
import com.example.jh352160.surfingtv.network.item.MyDataItem;
import com.example.jh352160.surfingtv.network.retrofit.CommonRetrofit;
import com.example.jh352160.surfingtv.network.service.MyDataService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by jh352160 on 2016/7/26.
 */

public class MypageMain extends Fragment implements View.OnClickListener {

    private static final int NETWORK_OK = 0;
    MypageItem item1, item2, item3, item4;
    ImageView imageView;
    View view;
    TextView userName;
    String age, name, photoUrl;
    ImageView sex;
    boolean isMale;
    public static LoginItem.DataBean userData = null;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case NETWORK_OK:
                    init();
            }
        }
    };

    public static LoginItem.DataBean getUserData() {
        return userData;
    }

    public static void setUserData(LoginItem.DataBean userData) {
        MypageMain.userData = userData;
    }

    public MypageMain() {
    }

    public static MypageMain newInstance() {
        return new MypageMain();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = inflater.inflate(R.layout.mypage_page, container, false);
        if (userData != null) {
            init();
        } else {
            connectNetWork();
        }
        return view;
    }

    private void init() {
        item1 = (MypageItem) view.findViewById(R.id.mypageItem1);
        item2 = (MypageItem) view.findViewById(R.id.mypageItem2);
        item3 = (MypageItem) view.findViewById(R.id.mypageItem3);
        item4 = (MypageItem) view.findViewById(R.id.mypageItem4);
        sex = (ImageView) view.findViewById(R.id.sex);
        imageView = (ImageView) view.findViewById(R.id.editImage);
        ProfileImageView profile = (ProfileImageView) view.findViewById(R.id.profile);
        userName = (TextView) view.findViewById(R.id.userName);
        item1.setOnClickListener(this);
        item1.setOnClickListener(this);
        item2.setOnClickListener(this);
        item3.setOnClickListener(this);
        item4.setOnClickListener(this);
        imageView.setOnClickListener(this);
        item2.setView(R.drawable.mypage_item_icon2, "意见反馈");
        item3.setView(R.drawable.mypage_item_icon3, "设置");
        item4.setView(R.drawable.mypage_item_icon4, "关于我们");
        if (!isMale) {
            sex.setImageResource(R.drawable.mypage_female);
        }
        Glide.with(getActivity()).load("http://app4tv.sudaotech.com/platform/image/" + userData.getProfile().getPhoto()).placeholder(R.drawable.mypage_portrait).into(profile);
        userName.setText(userData.getProfile().getNickname());
        name = userData.getProfile().getNickname();
        age = userData.getProfile().getAge() + "";
        isMale = userData.getProfile().getGender() == 1;
        photoUrl = userData.getProfile().getPhoto();
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.mypageItem1:
                intent = new Intent(this.getActivity(), Favorites.class);
                break;
            case R.id.mypageItem2:
                intent = new Intent(this.getActivity(), AdvicePage.class);
                break;
            case R.id.mypageItem3:
                intent = new Intent(this.getActivity(), MypageSetting.class);
                break;
            case R.id.mypageItem4:
                intent = new Intent(this.getActivity(), About_us_page.class);
                break;
            case R.id.editImage:
                intent = new Intent(this.getActivity(), EditPage.class);
                intent.putExtra("name", name);
                intent.putExtra("age", age);
                intent.putExtra("isMale", isMale);
                intent.putExtra("photoUrl", photoUrl);
                break;
        }
        startActivity(intent);
    }

    private void connectNetWork() {
        Retrofit retrofit = CommonRetrofit.getRetrofit();
        MyDataService myDataService = retrofit.create(MyDataService.class);
        Call<MyDataItem> call = myDataService.getItem();
        call.enqueue(new Callback<MyDataItem>() {
            @Override
            public void onResponse(Call<MyDataItem> call, Response<MyDataItem> response) {
                userData = new LoginItem.DataBean();
                userData.setProfile(new LoginItem.DataBean.ProfileBean());
                userData.getProfile().setPhoto(response.body().getData().getPhoto());
                userData.getProfile().setNickname(response.body().getData().getNickname());
                name = response.body().getData().getNickname();
                age = response.body().getData().getAge() + "";
                isMale = response.body().getData().getGender() == 1;
                photoUrl = response.body().getData().getPhoto();
                Message message = new Message();
                message.what = NETWORK_OK;
                handler.sendMessage(message);
            }

            @Override
            public void onFailure(Call<MyDataItem> call, Throwable t) {

            }
        });
    }
}
